package ru.ifmo.mlarinsky.classificator.main;

import net.miginfocom.swing.MigLayout;
import ru.ifmo.mlarinsky.classificator.accuracy.ClassificationEfficiencyCalculator;
import ru.ifmo.mlarinsky.classificator.classification.Classification;
import ru.ifmo.mlarinsky.classificator.classification.DJClassification;
import ru.ifmo.mlarinsky.classificator.classification.SPClassification;
import ru.ifmo.mlarinsky.classificator.gui.MapPanel;
import ru.ifmo.mlarinsky.classificator.io.InstrumentLoader;
import ru.ifmo.mlarinsky.classificator.map.*;
import ru.ifmo.mlarinsky.classificator.model.Instrument;
import ru.ifmo.mlarinsky.classificator.model.QuoteDate;
import ru.ifmo.mlarinsky.classificator.providers.DeterminationVectorProvider;
import ru.ifmo.mlarinsky.classificator.providers.InstructionsProvider;
import ru.ifmo.mlarinsky.classificator.providers.ReturnHistoryVectorProvider;
import ru.ifmo.mlarinsky.classificator.providers.VectorProvider;

import javax.swing.*;
import java.util.List;

/**
 * @author Mikhail Larinskiy
 */
public class Main {
    public static void main(String[] args) {
//        testMapNodePanelWithDefaultInstructions();
        testMapNodePanelWithReturnRateHistoryVectorProvider();
    }

    private static void testMapNodePanelWithDefaultInstructions() {
        final SomMap testMap = MapBuilder.buildLabeledMap(InstructionsProvider.DEFAULTS);
        MapPainter mapPainter = new MapDifferancePainter();
        mapPainter.paintMap(testMap);

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MapPanel mapPanel = new MapPanel(testMap, new DJClassification());

                JPanel contentPane = new JPanel(new MigLayout("fill, ins 0"));
                contentPane.add(mapPanel, "grow, push");

                JFrame mainFrame = new JFrame("Test Som Map");
                mainFrame.setContentPane(contentPane);
                mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                mainFrame.setSize(mapPanel.getPreferredSize().width, mapPanel.getPreferredSize().height + 15);
                mainFrame.setVisible(true);
            }
        });
    }

    private static void testMapNodePanelWithReturnRateHistoryVectorProvider() {
//        VectorProvider vectorProvider =
//                new ReturnHistoryVectorProvider(Collections.singletonList(InstrumentLoader.loadInstrument("YHOO",
//                        IOUtil.INSTRUMENTS_DATA_FILE_PATH + "yhoo.csv", InstrumentLoader.Period.DEFAULT)),
//                        QuoteDate.valueOf("01/01/2011"), 6);
        VectorProvider vectorProvider = new ReturnHistoryVectorProvider(InstrumentLoader.SAMPLE_INSTRUMENTS,
                QuoteDate.valueOf("01/01/2011"), 150);

        int dim = 20;

        InstructionsProvider provider = new InstructionsProvider();
        provider.setVectorProvider(vectorProvider);
        provider.setRadius(dim);
        provider.setXDimension(dim);
        provider.setYDimension(dim);
        provider.setSteps(1000);
        provider.setLearningRate(0.1);
        provider.setLearningRateType(InstructionsProvider.LearningRateType.EXPONENTIAL);
        provider.setNeighbourhoodType(InstructionsProvider.NeighbourhoodType.GAUSSIAN);
        provider.setLatticeType(InstructionsProvider.LatticeType.RECTANGULAR);
        provider.setOutputMapName("Return Rate History Map");
        provider.setNormalizationUsed(false);

        final SomMap testMap = MapBuilder.buildLabeledMap(provider);
        final Classification classification = new DJClassification();
//        final Classification classification = new SPClassification();
        MapDeterminationPainter painter = new MapDeterminationPainter(classification);

        painter.paintMap(testMap);

        final int accuracy = getAccuracy(testMap, classification);

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MapPanel mapPanel = new MapPanel(testMap, classification);

                JPanel contentPane = new JPanel(new MigLayout("fill, ins 0"));
                contentPane.add(mapPanel, "grow, push");

                JFrame mainFrame = new JFrame("Financial Instruments Map");
                mainFrame.setContentPane(contentPane);
                mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                mainFrame.pack();
                mainFrame.setResizable(false);
                mainFrame.setVisible(true);
            }
        });
    }

    private static int getAccuracy(SomMap somMap, Classification classification) {
        InstructionsProvider instructionsProvider = somMap.getInstructionsProvider();
        VectorProvider mapVectorProvider = instructionsProvider.getVectorProvider();
        List<Instrument> indexes = classification.getIndexes();
        DeterminationVectorProvider vectorProvider = new DeterminationVectorProvider(
                mapVectorProvider.getInstruments(), indexes,
                mapVectorProvider.getPeriodStart(),
                mapVectorProvider.getPeriodLength(),
                1);

        return ClassificationEfficiencyCalculator.getAccuracy(vectorProvider, indexes);
    }
}
